//by Omar "0m4r" Adobati

var FriendFeedWidget = Class.create({
  
  initialize: function(feedID, option){
	this.feedID = feedID;
	ffrf = new FFReadFeed(this.feedID, option);
	this.url = ffrf.getURL();
	this.isNew = option.isNew;
	this.option = option
  }, 

  getFeeds: function(url){
	if (window.JSON) {
		delete Array.prototype.toJSON;
	}
	var feedObj = null;
	if(url==null) url = this.url;
	new Ajax.Request(url, {
		method: 'GET',
		onLoading: this.loading,
	    onSuccess: this.success.bind(this),
	    onException: this.exception,
	    onComplete: this.complete,
	    onFailure: this.error.bind(this)
	});
  },
  
  loading: function(){
	if($('loading')==undefined){  
	  var loading = new FFLoading();
	  loading.setLoadingText('LOADING...', true);
	  loading.start();
	}else{
	  $('loading').appear({duration: 2.0});	
	}
  },
  
  complete: function(){ 
	$('loading').fade({duration: 4.0});
  },
  
  exception: function(obj, exception){
	$('body').update(exception.name + '\n' + exception.message);
  },
  
  error: function(obj){
	  if(this.isNew==1)
	    centerMainElements();
	  var json = eval('(' + obj.responseText + ')');
	  var error = new Element('p');
      error.update('');
      error.id = 'noresults';
      error.className = 'error';
      error.insert('No results to display');
      error.insert('<br/>');
      error.insert('for FriendFeed username: <i>' + username + '</i>');
      error.insert('<br/>');
      error.insert('<br/>');
      error.insert('error message is: ' + json.errorCode);
      error.insert('<br/>');
      $('content').insert(error);	   
  },
  
  success: function(obj){
	if(this.isNew==1)
	  centerMainElements();
    var json = eval('(' + obj.responseText + ')');
    if(json.errorCode!=null){
      this.error();
    }else{	
	  var fObj = new FFFeedObject(json);
	  var last = isNew * fObj.getNumberOfEntries();
	  var go = last - fObj.getNumberOfEntries();
	  var eUI = null;
	  for(var k=0; k<fObj.getNumberOfEntries(); k++){
		  var id = go+k;
		  var e = new FFEntry(fObj.getEntry(k));
		  eUI  = new FFEntryUI(id, e);
		  $('content').insert(eUI.div);
		  if(k==0 && this.isNew>1 && eUI!=null){
			new Effect.ScrollTo($(eUI.div.id), {duration: 4.0, offset: -50});
			$(eUI.div.id).highlight({duration: 4.0});
			current = eUI.div.id;
		  }
	  }
	  if(this.isNew==1){
	    var more = new Element('div');
	    more.update('');
	    more = new Element('div');
	    more.id = 'moreFeed';
	    more.className = 'feed';
	    more.insert('More ' + fObj.getNumberOfEntries() + ' FriendFeeds');
	    var moreNum = this.option.num;
	    var moreStart = 16;
	    more.observe('click', function(){
		  start({num: moreNum, start:moreStart});
	    });
	    $('ffw').insert(more);
	  }
    }
  },
  
  getEntries: function(entries){
	  var entriesObj = new Array();
	  for(var k=0; feedObj.getNumberOfEntries(); k++){
		 var entry = getEntry(entries[k], k); 
		 entriesObj.pull(entry);
	  }
	  return entriesObj;
  },
  
  getEntryByID: function(entry, id){
	 return new FFEntry(entry, id);
  },
  
  getEntryByPosition: function(entry, pos){
	return new FFEntry(entry, pos);
  },
  
  reload: function(){ 
	ffw.getFeeds();  
  }

});